package tmi.li.testapp;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

/** Helper to the database, manages versions and creation */
public class DBAdapter {
	private static final String TAG ="tmi.li.testapp.DBAdapter";
	private static final String DATABASE_NAME = "txwthr.db";
	private static final int DATABASE_VERSION = 3;

	// Table name
	public static final String TABLE = "widgets";

	// Columns
	public static final String ID = "_id";
	public static final String FLCOLOR = "FlColor";
	public static final String FTCOLOR = "FtColor";
	public static final String FBCOLOR = "FbColor";
	public static final String BCOLOR = "BColor";
	public static final String CURRENT = "Current";
	public static final String DESC = "desc";
	public static final String DESC2 = "desc2";

	public static final String DATABASE_CREATE = "create table "+TABLE+" (_id integer primary key, "
		+ FLCOLOR+" text," 
		+ FTCOLOR+" text,"
		+ FBCOLOR+" text,"
		+ BCOLOR+" text,"
		+ CURRENT+" text,"
		+ DESC+" text,"
		+ DESC2+" text"
		+");";
	
	private Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;


	public DBAdapter(Context context) {
		this.context = context;
		DBHelper = new DatabaseHelper(context);
		db = DBHelper.getWritableDatabase();
	}

	public boolean insertWidget(Widget widget) 
    {
        if(db.insert(TABLE, null, widget.getValues()) == -1) return false;
        return true;
    }

	public Cursor getWidget(int id) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, TABLE, new String[] {
                		ID,
                		FLCOLOR, 
                		FTCOLOR,
                		FBCOLOR,
                		BCOLOR,
                		CURRENT,
                		DESC,
                		DESC2
                		}, 
                		ID + "=" + id, 
                		null,
                		null, 
                		null, 
                		null, 
                		null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
	
	//---updates a title---
    public boolean updateWidget(Widget widget) 
    {
        return db.update(TABLE, widget.getValues(), 
                         ID + "=" + widget._id, null) > 0;
    }

    public boolean deleteWidget(Widget widget) {
    	return db.delete(TABLE, ID + "=" + widget._id, null) > 0;
    }
    
	private static class DatabaseHelper extends SQLiteOpenHelper 
	{
		DatabaseHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			try {
				db.execSQL(DATABASE_CREATE);
			}catch(SQLException e) {
				
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, 
				int newVersion) 
		{
			Log.w(TAG, "Upgrading database from version " + oldVersion 
					+ " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS "+TABLE);
			onCreate(db);
		}
	}    

}

